package com.newtouch.krd_app.util;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

import java.util.List;

/**
 * author : TQ
 * time   : 2023/2/19
 * desc   : url 基础工具类
 */
public class BaseUrlUtil {
//    private String jsonUrl = "[{\n" +
//            "    name: '218',\n" +
//            "    baseUrl: 'http://192.168.10.133:84',\n" +
//            "    serverVueUrl: 'http://192.168.10.133:84/file-server',\n" +
//            "    websocketUrl: '192.168.10.133:84',\n" +
//            "    index: 0\n" +
//            "}, {\n" +
//            "    name: '172',\n" +
//            "    baseUrl: 'http://172.19.11.157:18080',\n" +
//            "    serverVueUrl: 'http://192.168.10.133:84/file-server',\n" +
//            "    websocketUrl: '172.19.11.157:18080',\n" +
//            "    index: 1\n" +
//            "}, {\n" +
//            "    name: '192',\n" +
//            "    baseUrl: 'http://218.6.242.123:18000/prod-api',\n" +
//            "    serverVueUrl: 'http://218.6.242.123:18000/file-server',\n" +
//            "    websocketUrl: '218.6.242.123:18000/prod-api',\n" +
//            "    index: 2\n" +
//            "}]";
    private String jsonUrl = "[{\n" +
        "    name: '218',\n" +
        "    baseUrl: 'http://218.6.242.123:18000/prod-api',\n" +
        "    serverVueUrl: 'http://218.6.242.123:18000/file-server',\n" +
        "    websocketUrl: '218.6.242.123:18000/prod-api',\n" +
        "    index: 0\n" +
        "}, {\n" +
        "    name: '172',\n" +
        "    baseUrl: 'http://172.19.11.157:18080',\n" +
        "    serverVueUrl: 'http://192.168.10.133:84/file-server',\n" +
        "    websocketUrl: '172.19.11.157:18080',\n" +
        "    index: 1\n" +
        "}, {\n" +
        "    name: '192',\n" +
        "    baseUrl: 'http://192.168.10.133:18080',\n" +
        "    serverVueUrl: 'http://192.168.10.133:84/file-server',\n" +
        "    websocketUrl: '192.168.10.133:18080',\n" +
        "    index: 2\n" +
        "}]";

    private static BaseUrlUtil mInstance;
    private List<LinkedTreeMap> urlList;

    private LinkedTreeMap currentUrl;

    private Context context;

    private String Mac;

    public boolean isConnect;

    /**
     * 单列模式获取
     * @return
     */
    public static synchronized BaseUrlUtil getInstance(){
        if (mInstance == null){
            mInstance = new BaseUrlUtil();
        }
        return mInstance;
    }

    /**
     * 初始化
     */
    public void init(Context context){
        urlList = new Gson().fromJson(jsonUrl,List.class);
        currentUrl = urlList.get(0);
        Mac = (String) SPUtils.get(context,"Mac","");
        Log.e("Mac",Mac);
        if (TextUtils.isEmpty(Mac)){
            Mac = DeviceUtil.getMac();
            if (!"02:00:00:00:00:00".equals(Mac)){
                SPUtils.put(context,"Mac",Mac);
            }
        }else{
            String currentMac = DeviceUtil.getMac();
            if (!"02:00:00:00:00:00".equals(currentMac)&&!currentMac.equals(Mac)){
                SPUtils.put(context,"Mac",currentMac);
                Mac = currentMac;
            }
        }

        this.context = context;

        int url_index = (int) SPUtils.get(context,"url_index",0);
        Log.e("url_index",url_index+"");
        if (-1<url_index&&url_index<urlList.size()){
            currentUrl = urlList.get(url_index);
        }else{
            String json = (String) SPUtils.get(context,"currentUrl","");
            if (json.equals("")){
                currentUrl = urlList.get(0) ;
            }else{
                currentUrl = new Gson().fromJson(json,LinkedTreeMap.class);
            }
        }
    }

    public List<LinkedTreeMap> getUrlList() {
        return urlList;
    }

    public LinkedTreeMap getCurrentUrl() {
        return currentUrl;
    }

    /**
     * 从缓存中获取postion 进行设置
     * @param postion
     */
    public void setCurrentUrl(int postion){
        if (-1<postion&&postion<urlList.size()){
            currentUrl = urlList.get(postion);
        }
    }

    public void setCurrentUrl(String ip){
        LinkedTreeMap linkedTreeMap = new LinkedTreeMap();

        linkedTreeMap.put("baseUrl","http://"+ip);
        linkedTreeMap.put("serverVueUrl","http://"+ip.replace("/prod-api","")+"/file-server");
        linkedTreeMap.put("websocketUrl",ip);
        SPUtils.put(context,"currentUrl",new Gson().toJson(linkedTreeMap));
        SPUtils.put(context,"url_index",3);
    }

    public String getMac() {
        return Mac;
    }
}
